package com.zretc.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.entity.Comments;
import com.zretc.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴凯
 */
@RestController
@RequestMapping(value = "/commentAdmin")
public class CommentsController {
    @Autowired
    private CommentsService commentsService;
    @RequestMapping(value = "commentSelect/{filmId}",method = RequestMethod.GET)
   //根据电影编号，查询一级评论和一级评论对应的用户
    public  List<Comments> commentSelect(@PathVariable(value = "filmId") Integer filmId){
        List<Comments> rootCommentsByFilmId = commentsService.getRootCommentsByFilmId(filmId);
        return rootCommentsByFilmId;
    }

    /**
     * 后台多条件 查询所有的一级评论 按照评论姓名(模糊)，评论内容（模糊），评分
     * @return
     */
    @RequestMapping(value = "/backRootcommentsSelect",method = RequestMethod.GET)
    //后台查询一级评论和一级评论对应的用户
    public   PageInfo<Comments> backRootcommentsSelect(HttpServletRequest request){
        // 1.获取请求参数
        String pageNum = request.getParameter("page");
        String pageSize = request.getParameter("limit");
        String accountName = request.getParameter("username");
        String commentsContent = request.getParameter("content");
        String commentsScore = request.getParameter("cid");
        // 2.调用业务层
        Map<String, String> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("accountName",accountName);
        map.put("commentsContent",commentsContent);
        map.put("commentsScore",commentsScore);
        PageInfo<Comments> allRootComment = commentsService.getAllRootComment(map);
        return allRootComment;
    }



    /**
     * 根据编号 批量删除评论
     * @param
     * @return
     */
    @RequestMapping(value = "/backRootcommentsDelete",method = RequestMethod.GET)
    String deleteRootComment(HttpServletRequest request){
        String commentsId = request.getParameter("commentsId");
        List<String> comentsId = new ArrayList<>();
        comentsId.add(commentsId);
        Integer integer = commentsService.deleteRootComment(comentsId);
        return "1";

    }



    /**
     * 添加一级评论
     * @param
     * @return
     */
    @RequestMapping(value = "/rootCommentsAdd",method = RequestMethod.GET)
    String addRootComment(HttpServletRequest request){
        String useId = request.getParameter("useId");
        String commentsContent = request.getParameter("commentsContent");
        String commentsScore = request.getParameter("commentsScore");
        if("".equals(commentsScore)){
            commentsScore=null;
        }
        String filmId = request.getParameter("filmId");
        Comments comments = new Comments();
        comments.setUserId(Integer.parseInt(useId));
        comments.setCommentsContent(commentsContent);
        comments.setFilmId(Integer.parseInt(filmId));
        comments.setCommentsScore((float) Integer.parseInt(commentsScore));
        Integer integer = commentsService.addRootComment(comments);
        if (integer == 0){
            return "0";
        }else{
            return "1";//添加评论成功
        }




    };
}
